from browser import document
from browser import html
from browser import ajax
from browser import confirm
from browser import alert
import json
import time

hostsManageBtn = document["hostsManageBtn"]

hostTableTitle = ["Id","Addr","Username","CreateDate","UpdateDate","Remask","Opeation"]


def whiteTitle(titleName):
    thead = html.THEAD()
    tr = html.TR()

    for i in range(len(titleName)):
        th = html.TH(hostTableTitle[i])
        tr <= th

    thead <= tr
    return thead

def delhostsComplete(req):
    alert(req.text)
    workWindow.clear()
    showHostsInfos()


def delHosts(ev):
    btn = ev.target
    hostid = btn.dataset["id"]

    if confirm("确定删除主机 " + hostid + " 信息么?"):
        data = {
            "delhostid": hostid
        }

        request = ajax.Ajax()
        request.bind("complete", delhostsComplete)
        request.open("POST", "/manageHosts", False)
        request.send(data)

def showHostEv(req):
    if req.status == 200:
        data = json.loads(req.text)

        if 0 == data["Code"]:
            if None == data["Data"]:
                return

            tbody = html.TBODY()
            for line in data["Data"]:

                tr = html.TR()

                for k in hostTableTitle:
                    if k in line:
                        if k == "Id":
                            td = html.TH(line[k])

                        elif k == "CreateDate" or k == "UpdateDate":
                            td = html.TD(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(line[k]))))

                        else:
                            td = html.TD(line[k])

                    elif k == "Opeation":
                        UpdateA = html.A()
                        UpdateA.href = "./UpdateHosts.html?id=" + str(line["Id"])

                        UpdateBtn = html.BUTTON("update")
                        UpdateBtn.className = "btn btn-cyan btn-round btn-xs"

                        UpdateA <= UpdateBtn

                        DelBtn = html.BUTTON("delete")
                        DelBtn.className = "btn btn-danger btn-round btn-xs"
                        DelBtn.dataset["id"] = line["Id"]

                        DelBtn.bind("click", delHosts)

                        td = html.TD(UpdateA + " " + DelBtn)

                    tr <= td

                tbody <= tr
                hostsTable <= tbody


# def hostadd(ev):

    # workWindow.clear()

    # addHostA = html.A()
    # addHostA.href = "./addHost.html"
    # addHostA.target = "_blank"
    #
    # addHostBtn = html.BUTTON("AddUser")
    # addHostBtn.type = "button"
    # addHostBtn.className = "btn btn-primary btn-sm"
    #
    # addHostA <= addHostBtn
    # workWindow <= html.DIV(addHostA)

    # workWindow.clear()
    #
    # addUserA = html.A()
    # addUserA.href = "./addUsers.html"
    # addUserA.target = "_blank"
    #
    # addUserBtn = html.BUTTON("AddUser")
    # addUserBtn.type = "button"
    # addUserBtn.className = "btn btn-primary btn-sm"
    #
    # addUserA <= addUserBtn
    #
    #
    # workWindow <= html.DIV(addUserA)


    # workWindow <= html.DIV(addHostA)

def showHostsInfos():
    workWindow.clear()

    addHostA = html.A()
    # addHostA.href = "#"
    addHostA.href = "./addHost.html"
    # addHostA.bind("click",hostadd)

    addHostBtn = html.BUTTON("AddHosts")
    addHostBtn.type = "button"
    addHostBtn.className = "btn btn-primary btn-sm"

    addHostA <= addHostBtn


    workWindow <= html.DIV(addHostA)

    global hostsTable
    hostsTable = html.TABLE()
    hostsTable.className = "table table-hover"

    hostsTable <= whiteTitle(hostTableTitle)
    workWindow <= hostsTable

    request = ajax.Ajax()
    request.bind("complete",showHostEv)
    request.open("GET","/showHosts",False)
    request.send()

def hostsManage(ev):
    showHostsInfos()

workWindow = document["workWindow"]

hostsManageBtn.bind("click",hostsManage)
